package com.ruoyi.web.controller.member;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.RecordType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.XxMemberAmountRecord;
import com.ruoyi.system.domain.request.XxMemberAmountRecordRequest;
import com.ruoyi.system.domain.vo.XxMemberAmountRecordVo;
import com.ruoyi.system.service.IXxMemberAmountRecordService;
import com.ruoyi.system.service.IXxMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 会员金额记录Controller
 * 
 * @author ruoyi
 * @date 2025-02-08
 */
@RestController
@RequestMapping("/system/record")
public class XxMemberAmountRecordController extends BaseController
{
    @Autowired
    private IXxMemberAmountRecordService xxMemberAmountRecordService;

    @Autowired
    private IXxMemberService xxMemberService;

    /**
     * 查询会员金额记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:record:list')")
    @GetMapping("/list")
    public TableDataInfo list(XxMemberAmountRecordRequest request)
    {
        startPage();
        List<XxMemberAmountRecordVo> list = xxMemberAmountRecordService.selectXxMemberAmountRecordList(request);
        return getDataTable(list);
    }

    /**
     * 导出会员金额记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:record:export')")
    @Log(title = "会员金额记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, XxMemberAmountRecordRequest request)
    {
        List<XxMemberAmountRecordVo> list = xxMemberAmountRecordService.selectXxMemberAmountRecordList(request);
        ExcelUtil<XxMemberAmountRecordVo> util = new ExcelUtil<XxMemberAmountRecordVo>(XxMemberAmountRecordVo.class);
        util.exportExcel(response, list, "会员金额记录数据");
    }

    /**
     * 获取会员金额记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:record:query')")
    @GetMapping(value = "/{recordId}")
    public AjaxResult getInfo(@PathVariable("recordId") Long recordId)
    {
        return success(xxMemberAmountRecordService.selectXxMemberAmountRecordByRecordId(recordId));
    }

    /**
     * 新增会员金额记录
     */
    @PreAuthorize("@ss.hasPermi('system:record:add')")
    @Log(title = "会员金额记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody XxMemberAmountRecord xxMemberAmountRecord)
    {
        xxMemberAmountRecord.setCreateBy(getUsername());
        if(RecordType.CONSUME.getCode().equals(xxMemberAmountRecord.getRecordType())) {
            // 检查余额是否充足
            xxMemberService.checkBalance(xxMemberAmountRecord.getMemberId(), xxMemberAmountRecord.getRecordAmount());
        }
        return toAjax(xxMemberAmountRecordService.insertXxMemberAmountRecord(xxMemberAmountRecord));
    }

    /**
     * 修改会员金额记录
     */
    @PreAuthorize("@ss.hasPermi('system:record:edit')")
    @Log(title = "会员金额记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody XxMemberAmountRecord xxMemberAmountRecord)
    {
        return toAjax(xxMemberAmountRecordService.updateXxMemberAmountRecord(xxMemberAmountRecord));
    }

    /**
     * 删除会员金额记录
     */
    @PreAuthorize("@ss.hasPermi('system:record:remove')")
    @Log(title = "会员金额记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{recordIds}")
    public AjaxResult remove(@PathVariable Long[] recordIds)
    {
        return toAjax(xxMemberAmountRecordService.deleteXxMemberAmountRecordByRecordIds(recordIds));
    }
}
